package com.segi.view.refresh;

import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.segi.view.R;

import cn.segi.framework.log.Logger;
import cn.segi.framework.util.WindowDispaly;

/**
 * 这个类封装了下拉刷新的布局
 *
 * @author liangzx
 * @version [segi, 2013-12-23]
 */
public class HeaderLoadingLayoutForUhomeNew extends LoadingLayout {

    /**
     * Header的容器
     */
    private LinearLayout mHeaderContainer;

    /**
     * 下拉第一战图
     */
    private ImageView sunImage;

    private ImageView mFirstImg;
    /**
     * 进度条
     */
    private ImageView mProgressBar;
    /**
     * 状态提示TextView
     */
    private TextView mHintTextView;
    /**
     * 第一张图的高度
     **/
    private int mFirstImgHeight;

    /**
     *
     */
    private AnimationDrawable mProgressAnim;

    /**
     * 构造方法
     *
     * @param context context
     */
    public HeaderLoadingLayoutForUhomeNew(Context context) {
        super(context);
        init(context);
    }

    /**
     * 构造方法
     *
     * @param context context
     * @param attrs   attrs
     */
    public HeaderLoadingLayoutForUhomeNew(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    /**
     * 初始化
     *
     * @param context context
     */
    private void init(Context context) {
        mHeaderContainer = (LinearLayout) findViewById(R.id.header_container);
        mHintTextView = (TextView) findViewById(R.id.header_hint_text);
        sunImage = (ImageView) findViewById(R.id.animation_sun);
        mFirstImg = (ImageView) findViewById(R.id.animation_windmill);
        mProgressBar = (ImageView) findViewById(R.id.animation_windmill_active);

        mProgressAnim = (AnimationDrawable) mProgressBar.getDrawable();

        int height = (int) ((160 / 750f) * WindowDispaly.getWidth());
        int width = WindowDispaly.getWidth();


        FrameLayout.LayoutParams params4 = (FrameLayout.LayoutParams) sunImage.getLayoutParams();
        params4.height = (int) ((38 / 750f) * WindowDispaly.getWidth());
        params4.width = (int) ((38 / 750f) * WindowDispaly.getWidth());
        params4.setMargins((int) (232 * WindowDispaly.getWidth() / 750f), (int) (height * 0.8f), 0, 0);
        sunImage.setLayoutParams(params4);

        LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) findViewById(R.id.header_content).getLayoutParams();
        params.height = height;
        params.width = WindowDispaly.getWidth();
        findViewById(R.id.header_content).setLayoutParams(params);


        FrameLayout.LayoutParams params2 = (FrameLayout.LayoutParams) mFirstImg.getLayoutParams();
        params2.height = (int) ((114 / 750f) * WindowDispaly.getWidth());
        params2.width = (int) ((114 / 750f) * WindowDispaly.getWidth());
        params2.setMargins((int) ((width / 750f) * 341), (int) ((height / 160f) * 19), 0, 0);
        mFirstImg.setLayoutParams(params2);

        FrameLayout.LayoutParams params3 = (FrameLayout.LayoutParams) mProgressBar.getLayoutParams();
        params3.height = (int) ((114 / 750f) * WindowDispaly.getWidth());
        params3.width = (int) ((114 / 750f) * WindowDispaly.getWidth());
        params3.setMargins((int) ((width / 750f) * 341), (int) ((height / 160f) * 19), 0, 0);
        mProgressBar.setLayoutParams(params3);

    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
        int w = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        int h = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED);
        mHeaderContainer.measure(w, h);
        mFirstImgHeight = mHeaderContainer.getMeasuredHeight();

    }

    @Override
    public void setLastUpdatedLabel(CharSequence label) {
        // 如果最后更新的时间的文本是空的话，隐藏前面的标题
    }

    @Override
    public int getContentSize() {
//        if (0 < mFirstImgHeight) {
//            return mFirstImgHeight;
//        }
        if (null != mHeaderContainer) {
            return mHeaderContainer.getHeight();
        }

        return (int) (getResources().getDisplayMetrics().density * 60);
    }

    @Override
    protected View createLoadingView(Context context, AttributeSet attrs) {
        View container = LayoutInflater.from(context).inflate(R.layout.pull_updown_header_for_uhome_new, null);
        return container;
    }

    @Override
    protected void onStateChanged(State curState, State oldState) {
        super.onStateChanged(curState, oldState);
    }

    @Override
    protected void onReset() {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mHeaderContainer.getLayoutParams();
        params.height = mFirstImgHeight;
        mHeaderContainer.setLayoutParams(params);
        stopAnim();
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_normal);
    }

    @Override
    public void onPull(float scale) {
        if (getState().equals(State.REFRESHING) || getPreState().equals(State.REFRESHING)) {
            return;
        }

        if (scale < 1) {
            //这个时候就是设置太阳的移动高度
            //需要设置太阳移动的高度

            //风车中心398：750 76：160
            //top = 76- 114/2 = 19
            //left = 398- 114/2 = 341

            //太阳 中心 250:750
            //top = 0.2*height

            //left = 250-38 = 232

            FrameLayout.LayoutParams params2 = (FrameLayout.LayoutParams) sunImage.getLayoutParams();
            //0.8*height - x*1.3*heaight
            params2.setMargins((int) (232 * WindowDispaly.getWidth() / 750f), (int) (mFirstImgHeight * 0.9 - scale * mFirstImgHeight * 0.8), 0, 0);
            sunImage.setLayoutParams(params2);
        } else if (scale == 1) {
            FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mHeaderContainer.getLayoutParams();
            params.height = mFirstImgHeight;
            mHeaderContainer.setLayoutParams(params);

            FrameLayout.LayoutParams params2 = (FrameLayout.LayoutParams) sunImage.getLayoutParams();
            params2.setMargins((int) (232 * WindowDispaly.getWidth() / 750f), (int) (mFirstImgHeight * 0.1), 0, 0);
            sunImage.setLayoutParams(params2);

        }

    }

    @Override
    protected void onPullToRefresh() {
        stopAnim();
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_normal);
    }

    @Override
    protected void onReleaseToRefresh() {
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_ready);
    }

    @Override
    protected void onRefreshing() {

        FrameLayout.LayoutParams params2 = (FrameLayout.LayoutParams) sunImage.getLayoutParams();
        //0.9*height - x*1.3*heaight
        params2.setMargins((int) (232 * WindowDispaly.getWidth() / 750f), (int) (mFirstImgHeight * 0.9 - 1 * mFirstImgHeight * 0.8), 0, 0);
        sunImage.setLayoutParams(params2);

        startAnim();
        mHintTextView.setText(R.string.pull_to_refresh_header_hint_loading);
    }

    /**
     * 开始动画
     */
    private void startAnim() {
        FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) mHeaderContainer.getLayoutParams();
        params.height = mFirstImgHeight;
        mHeaderContainer.setLayoutParams(params);
        mFirstImg.setVisibility(View.INVISIBLE);
        mProgressBar.setVisibility(View.VISIBLE);
        if (!mProgressAnim.isRunning()) {
            mProgressAnim.start();
        }
    }

    /**
     * 暂停动画
     */
    private void stopAnim() {
        mFirstImg.setVisibility(View.VISIBLE);
        mProgressBar.setVisibility(View.INVISIBLE);
        if (mProgressAnim.isRunning()) {
            mProgressAnim.stop();
        }
    }
}
